import { initState } from "./state";
import { compileToRenderFunction } from './compiler';
import { mountComponent } from './lifecycle';

function initMixin(Vue) {
    Vue.prototype._init = function (options) {
        var vm = this;
        vm.$options = options;
        initState(vm);

        // 挂在函数 Vue.prototype.$mount
        if (vm.$options.el) {
            vm.$mount(vm.$options.el)
        }
    }

    Vue.prototype.$mount = function (el) {
        const vm = this,
            options = vm.$options;

        el = document.querySelector(el),
            vm.$el = el;

        if (!options.render) {
            let template = options.template;

            if (!template && el) {
                template = el.outerHTML;
            }

            // 把模板变为render函数 template -> AST树 -> render 函数
            const render = compileToRenderFunction(template);
            options.render = render;
        }

        mountComponent(vm);
    }
}


export {
    initMixin
}